package com.controller;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.dto.Result;
import com.baomidou.kisso.dto.ResultGenerator;
import com.baomidou.kisso.security.token.SSOToken;
import com.baomidou.kisso.service.ym.CookieService;
import com.baomidou.kisso.service.ym.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * boot的测试控制器
 *
 * @author fuminwang
 * @create 2017-08-29 10:05
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping("/ym")
public class ModuleOneController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;
    @Autowired
    CookieService cookieService;
    @Autowired
    LoginService loginService;

    // 授权登录
    @ResponseBody
    @RequestMapping("/login")
    @Login
    public String login() {
        // 设置登录 COOKIE
        String account = "13718102718";
        String password = "ruby1207";
        loginService.login(account,password,request,response);
        //SSOHelper.setCookie(request, response, SSOToken.create().setIp(request).setId(1000).setIssuer("kisso"), false);
        return "login success!";
    }

    // 查看登录信息
    @ResponseBody
    @RequestMapping("/token")
    public Result token() {
        return ResultGenerator.genSuccessResult(cookieService.getCookieAttr(request));
    }

    // 退出登录
    @ResponseBody
    @RequestMapping("/logout")
    public String logout() {
        SSOHelper.clearLogin(request, response);
        return "Logout Kisso!";
    }
}
